package chapter01.d04;
/**
 * Wait、nofify使用案例
 * @author kenneth
 *
 */
public class ThreadWait {
	public static void main(String[] args) {
		MyThread mt = new MyThread();
		mt.start();
		try {
			Thread.currentThread().sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		mt.suspendThread();
		System.out.println("唤醒mt线程");
		try {
			Thread.currentThread().sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		mt.resumeThread();
	}
}

class MyThread extends Thread {
	public boolean stop = false;

	public void run() {
		while (true) {
			synchronized (this) {
				System.out.println("running....");
				if (stop)
					try {
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
			}
		}
	}

	public void suspendThread() {
		this.stop = true;
	}

	public void resumeThread() {
		synchronized (this) {
			this.stop = false;
			notify();
		}
	}
}
